Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
90 lines (79 loc) · 3.56 KB

8 - WebSocket/Server.md

File metadata and controls

90 lines (79 loc) · 3.56 KB

WebSocket\Server

1.7.9增加了内置的WebSocket服务器支持,通过几行PHP代码就可以写出一个异步非阻塞多进程的WebSocket服务器。

$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();

onRequest回调

WebSocket\Server 继承自 Http\Server

  • 设置了onRequest回调,WebSocket\Server也可以同时作为http服务器
  • 未设置onRequest回调,WebSocket\Server收到http请求后会返回http 400错误页面
  • 如果想通过接收http触发所有websocket的推送,需要注意作用域的问题,面向过程请使用globalWebSocket\Server进行引用,面向对象可以把WebSocket\Server设置成一个成员属性

1、面向过程代码

$server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
		echo "server: handshake success with fd{$request->fd}\n";
	});
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
		echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
		$server->push($frame->fd, "this is server");
	});
$server->on('close', function ($ser, $fd) {
		echo "client {$fd} closed\n";
	});
$server->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) {
		global $server;//调用外部的server
		// $server->connections 遍历所有websocket连接用户的fd,给所有用户推送
		foreach ($server->connections as $fd) {
			$server->push($fd, $request->get['message']);
		}
	});
$server->start();

2、面向对象代码

class WebsocketTest {
	public $server;
	public function __construct() {
		$this->server = new Swoole\WebSocket\Server("0.0.0.0", 9501);
		$this->server->on('open', function (swoole_websocket_server $server, $request) {
			echo "server: handshake success with fd{$request->fd}\n";
		});
		$this->server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
			echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
			$server->push($frame->fd, "this is server");
		});
		$this->server->on('close', function ($ser, $fd) {
			echo "client {$fd} closed\n";
		});
		$this->server->on('request', function ($request, $response) {
			// 接收http请求从get获取message参数的值,给用户推送
			// $this->server->connections 遍历所有websocket连接用户的fd,给所有用户推送
			foreach ($this->server->connections as $fd) {
				$this->server->push($fd, $request->get['message']);
			}
		});
		$this->server->start();
	}
}
new WebsocketTest();

客户端

  • Chrome/Firefox/高版本IE/Safari等浏览器内置了JS语言的WebSocket客户端
  • 微信小程序开发框架内置的WebSocket客户端
  • 异步的PHP程序中可以使用Swoole\Http\Client作为WebSocket客户端
  • apache/php-fpm或其他同步阻塞的PHP程序中可以使用swoole/framework提供的同步WebSocket客户端
  • WebSocket客户端不能与WebSocket服务器通信